#include "avro_napi.h"
#include "../utils/ohoslog/ohos_log.h"
#include "native_common.h"

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        DECLARE_NAPI_FUNCTION("parse", OhosAvroC::Parse),
        DECLARE_NAPI_FUNCTION("toBuffer", OhosAvroC::ToBuffer),
        DECLARE_NAPI_FUNCTION("getName", OhosAvroC::GetSchemaTypeName),
        DECLARE_NAPI_FUNCTION("getArrayItemType", OhosAvroC::GetArrayItemType),
        DECLARE_NAPI_FUNCTION("getMapValueType", OhosAvroC::GetMapValueType),
        DECLARE_NAPI_FUNCTION("getFixedSize", OhosAvroC::GetFixedSize),
        DECLARE_NAPI_FUNCTION("getSchema", OhosAvroC::GetSchema),
        DECLARE_NAPI_FUNCTION("getAliases", OhosAvroC::GetAliases),
        DECLARE_NAPI_FUNCTION("fromBuffer", OhosAvroC::FromBuffer),
        DECLARE_NAPI_FUNCTION("isValid", OhosAvroC::IsValid),
        DECLARE_NAPI_FUNCTION("compareBuffers", OhosAvroC::CompareBuffers),
    };
    napi_value avro = nullptr;
    napi_status status;
    napi_ref *ref = new napi_ref;
    const char *classBindName = "OhosAvroC";
    status = napi_define_class(env, classBindName, NAPI_AUTO_LENGTH, OhosAvroC::Construct, nullptr, sizeof(desc) / sizeof(desc[0]), desc, &avro);
    STATUS_CHECK(status);
    
    status = napi_create_reference(env, avro, 1, ref);
    STATUS_CHECK(status);
    
    status = napi_set_named_property(env, exports, classBindName, avro);
    STATUS_CHECK(status);

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    
    napi_value serializeToFileFunc;
    napi_create_function(env, "toFile", NAPI_AUTO_LENGTH, SerializeToFile, nullptr, &serializeToFileFunc);
    napi_set_named_property(env, exports, "toFile", serializeToFileFunc);

    napi_value fromFileFunc;
    napi_create_function(env, "fromFile", NAPI_AUTO_LENGTH, FromFile, nullptr, &fromFileFunc);
    napi_set_named_property(env, exports, "fromFile", fromFileFunc);
    
    return exports;
}
EXTERN_C_END

//static napi_module avroModule = {
//    .nm_version = 1,
//    .nm_flags = 0,
//    .nm_filename = nullptr,
//    .nm_register_func = Init,
//    .nm_modname = "avro_napi",
//    .nm_priv = ((void*)0),
//    .reserved = { 0 },
//};
//
//extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
//{
//    napi_module_register(&avroModule);
//}

// 模块注册（固定格式）
NAPI_MODULE(avro_napi, Init)
